package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.Process;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;


/**
 * @Description:
 * @Author: wang fei long
 * @Date: 2025/9/17 0017 - 16:10
 */
@Service
public class ProcessServiceImpl implements ProcessService {
    @Autowired
    private ProcessMapper processMapper;

    @Override
    public Result<PageInfo<Process>> selectAll(Process process, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<Process> processes = processMapper.selectAll(process);
        return Result.success(new PageInfo<>(processes));
    }

    @Override
    public Result insertProcess(Process process) {
        if (process.getProcessCode() == null || "".equals(process.getProcessCode()) ||
                process.getProcessName() == null || "".equals( process.getProcessName()) ||
                process.getProcessDescription() == null || "".equals( process.getProcessDescription()) ||
                process.getRemarks() == null || "".equals( process.getRemarks()) ||
                process.getIsActive() == null){
            return Result.error("添加失败，输入信息不完整");
        }
        Process process1 = processMapper.selectByProcessCode(process.getProcessCode());
        if (process1 != null){
            return Result.error("添加失败，id有重复");
        }else {
            return processMapper.insertProcess(process) ==1?Result.success(process):Result.error("操作失败");
        }
    }

    @Override
    public Result<Process> selectByCode(String processCode) {
        return Result.success(processMapper.selectByCode(processCode));
    }

    @Override
    public Result updateById(Process process) {
        if (process.getProcessCode() == null || "".equals(process.getProcessCode()) ||
                process.getProcessName() == null || "".equals( process.getProcessName()) ||
                process.getProcessDescription() == null || "".equals( process.getProcessDescription()) ||
                process.getRemarks() == null || "".equals( process.getRemarks()) ||
                process.getIsActive() == null){
            return Result.error("修改失败，修改信息不完整");
        }
        Process process1 = processMapper.selectByProcessCode(process.getProcessCode());
        if (process1 != null && process1.getId() != process.getId()){
            return Result.error("修改失败，id有重复");
        }else {
            return processMapper.updateById(process) ==1?Result.success():Result.error("操作失败");
        }
    }

    @Override
    public Result deleteById(Long id) {
        return processMapper.deleteById(id)==1?Result.success():Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return processMapper.batchDelete(ids) > 0?Result.success():Result.error("操作失败");
    }
}